package com.wjbgn.electronicnian.scheduler;

import com.wjbgn.electronicnian.entity.NianDO;
import com.wjbgn.electronicnian.service.NianService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 饥饿值扣除任务
 *
 * @author weirx
 * @date 2022/01/22 12:09
 **/
@Slf4j
@Component
public class HungryNumTask {

    @Autowired
    private NianService nianService;

    @Scheduled(cron = "0 */30 * * * ?")
    public void execute() {
        log.info("执行定时扣除老虎饥饿值任务。。");
        // 获取所有的年兽
        List<NianDO> list = nianService.lambdaQuery().gt(NianDO::getHungryNum, 0 ).list();
        list.forEach(a->{
            a.setHungryNum(a.getHungryNum() -1 );
        });
        nianService.updateBatchById(list);

    }
}
